<?php
namespace Admin\Controller;
use Think\Controller;
class ProductsController extends CommonController 
{	
	public $product ;

	public $pro_cate ;

	public $cateCon ;

	public function _initialize()
	{
		$this->product = M('product');

		$this->pro_cate = M('pro_cate');

		$this->cateCon = new CategoryController();
	}


	public function showlist()
	{
		//$data = M('user  as  a')->join('jifen  as  b  on b.id = a.id')->where('a.id  =  1')->select();

		if(IS_POST){

		}else{
			$p = isset($_GET['p'])?$_GET['p']:1;

			$products = M('product as a');

			$field = 'a.pro_id,a.pro_name,a.create_time,c.catename';

			$list = $products->field($field)->join('rl_pro_cate as b on a.pro_id = b.pro_id')->join('rl_category as c on c.cate_id = b.cate_id')->page($p,10)->order('a.create_time desc')->select();
			foreach($list as $k => $v){
				$list[$k]['create_time'] = date('Y-m-d',$v['create_time']);
			}
			$count = $products->count();
			$page = new \Think\Page($count,10);

			
			$show = $page->show();
			

			// var_dump($page);die;
			
			$this->assign('page',$show);
			$this->assign('list',$list);
			$this->display();
		}
		
	}

	public function append()
	{

		//调用分类控制器

		$this->assign('cate_list',$this->cateCon->cate_list);

		if(IS_AJAX){

			$data = I('post.');
			$data['create_time'] = time();

			if (!$this->product->create()){
			     // 如果创建失败 表示验证没有通过 输出错误提示信息
		    	$info['code'] = '0';
				$info['info'] = $this->product->getError();
				$this->ajaxReturn($info);
			}else{
				if($pro_id =$this->product->add($data)){
					//插入产品分类表数据
					$pr_ca_data['pro_id'] = $pro_id;
					$pr_ca_data['cate_id'] = I('post.category');

					if($this->pro_cate->add($pr_ca_data)){
						//分类表插入成功
						$info['code'] = '1';
						$info['info'] = '添加成功';
						$info['status'] = 'success';
						$this->ajaxReturn($info);
					}else{

						$this->ajaxReturn(['info'=>$this->pro_cate->getError]);
					}
					
					
				}else{
					$info['code'] = '0';
					$info['info'] = '添加失败';
					$info['status'] = 'error';
					$this->ajaxReturn($info);
				}
				// $this->pro_cate->add();

			}

		}
		$this->display();

	}

	public function edit()
	{	

		$this->assign('cate_list',$this->cateCon->cate_list);//获取到分类列表

		$pro_id = I('get.pro_id');//获取当前修改ID

		$data = $this->product->join('rl_pro_cate on rl_pro_cate.pro_id = rl_product.pro_id')->join('rl_category on rl_category.cate_id = rl_pro_cate.cate_id')->where(array('rl_product.pro_id'=>$pro_id))->find(); //原数据查找
		
		// $data['cate_id'] = (int)$data['cate_id'];
		
		// var_dump($data);die;
		$cate_id = $data['cate_id'];
		$this->assign('cate_id',$cate_id);
		$this->assign('data',$data);//原数据
		
		if(IS_AJAX){


			//更新数据
			$data['pro_name'] = I('post.pro_name');
			$data['inventory'] = I('post.inventory');
			$data['cate_id'] = I('post.category');

			$pro_upd = $this->product->where(array('pro_id'=>$pro_id))->save($data);

			$pr_ca_upd = $this->pro_cate->where(array('pro_id'=>$pro_id))->save($data);

			// var_dump($manager->getLastSql());
			if($pro_upd || $pr_ca_upd){
				$info['code'] = 1;
				$info['info'] = '修改成功';
				$info['status'] = 'success';
				$this->ajaxReturn($info);
			}else{
				$info['code'] = 0;
				$info['info'] = '修改失败';
				$info['status'] = 'error';
				$this->ajaxReturn($info);
			}

		}

		$this->display();
	}

	public function del()
	{	
		if(IS_AJAX){

			

			$del = $this->product->where(array('pro_id'=>I('post.pro_id')))->delete();

			if($del){
				$info['code'] = 1;
				$info['info'] = '删除成功';
				$info['status'] = 'success';
				$this->ajaxReturn($info);

			}else{
				$info['code'] = 0;
				$info['info'] = '删除失败';
				$info['status'] ='error';
				$this->ajaxReturn($info);
			}

		}
	}

	
}

?>